﻿using OAuth.Dapper.Infrastructure.Data;
using OAuth.Dapper.Models;
using OAuth.Server.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OAuth.Server.Domain
{
    public class ClientRepository
    {
        private readonly DapperDBContext context;
        public ClientRepository(DapperDBContext context)
        {
            this.context = context;
        }


        public async Task<TClient> Queryclient(int id)
        {
          
            return await context.QueryFirstOrDefaultAsync<TClient>("SELECT * FROM TClient WHERE ", new { source = SecretSource.Client, sourceId = id });
        }
        public async Task<IEnumerable<TSecret>> QuerySecrets(int clientId)
        {
            var type = typeof(TSecret);
            var sql =
                $"SELECT * FROM secret WHERE Source=@source AND SourceId=@sourceId";
           // sql = Connection.FormatSql(sql);
            return await context.QueryAsync<TSecret>(sql, new { source = SecretSource.Client, sourceId = clientId });
        }

        public async Task<TClientConfig> QueryConfig(int clientId)
        {
            return await context.GetAsync<TClientConfig>(clientId);
        }

        public async Task<IDictionary<string, string>> QueryProperties(int clientId)
        {
            var type = typeof(TClientProperty);
            var models= await context.QueryAsync<TClientProperty>("select * from client_claim where ClientId=@clientId ",clientId);
            return models.ToDictionary(k => k.Key, v => v.Value);
        }
    }
}
